package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //查询所有地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        //查询地址簿,根绝当前用户唯一userId查询
        Long userId = BaseContext.getThreadId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    //新增收货地址
    @PostMapping
    public R<AddressBook> addAddress(@RequestBody AddressBook addressBook){
        //根据当前用户唯一userId存储addressBook
        addressBook.setUserId(BaseContext.getThreadId());
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    //设置默认地址
    @PutMapping("/default")
    public R<AddressBook> defaultAddress(@RequestBody AddressBook addressBook){
       log.info("addressBook:{}", addressBook);
       //现将所有地址isDefault字段改为0
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,BaseContext.getThreadId());
        qw.set(AddressBook::getIsDefault,0);
        addressBookService.update(qw);

        //再将设置的一个地址idDefault字段改为1
       addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getThreadId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.fail("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.fail("没有找到该对象");
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getThreadId());
        log.info("addressBook:{}", addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
